<?php

namespace Blog\Controller;

use Think\Controller;
use Think\Model;
use Think\Page;
use Think\Upload;
use Think\Verify;

class IndexController extends Controller
{


    public function _before_register()
    {
        if (IS_POST) {
            $u = D('User');
            $data = $u->create();
            //var_dump($data);

            if ($data) {
                $pa2 = $_POST['password2'];
                $pa = $_POST['password'];
                $email = $_POST['email'];
                //用户名在不在
                $c['username'] = $data['username'];
                if ($u->where($c)->count()) {
                    $this->error("用户名已存在");
                }
                var_dump($pa);
                var_dump($pa2);
                if ($pa == $pa2) {
                    //密码加密
                    $data['password'] = md5($data['password']);
                    $res = $u->add($data);

                } else {
                    $this->error('两次密码输入不匹配');
                }
                $this->success("注册成功", U("login"));
                exit;
            } else {
                $this->error($u->getError());
            }
        }
    }

    public function register()
    {
        $this->display();
    }

    public function checkUserAjax($name)
    {
        $m = M('User');
        $c['username'] = $name;
        $num = $m->where($c)->count();
        if ($num > 0) {
            $r['status'] = 0;
            $r['msg'] = '用户名已存在，不能注册';
        } else {
            $r['status'] = 1;
            $r['msg'] = '可以注册';
        }
        $this->ajaxReturn($r);

    }

    public function _before_login()
    {
        if (IS_POST) {
            $u = M('User');
            $vercode = new Verify();
            $data = $u->create();
            $value = $_POST['vercode'];
            $res = $vercode->check($value);
            if ($res) {
                $data['password'] = md5($data['password']);
                $userArr = $u->where($data)->find();
                if ($userArr) {
                    //写凭证
                    session('user_id', $userArr['id']);
                    //提示
                    $this->success('登录成功', U('index'));
                } else {
                    //提示用户名密码错误
                    $this->error('密码错误');

                }
                exit;
            } else {
                $this->error('验证码输入错误', U('login'));
            }

        }
    }

    public function vercode()
    {
        $vercode = new Verify();
        $vercode->entry();
    }

    public function login()
    {
        $this->display();
    }


    public function addBlog()
    {
        $this->display();
    }

    public function addBlogSave()
    {
        if (session('?user_id')) {
            $article = M('article');
            $arr = $article->create();
            $upload = new Upload();
            $upload->maxSize = 10 * 1024 * 1024;
            $upload->rootPath = './Uploads/';
            $res = $upload->uploadOne($_FILES['url']);
            if ($res) {
                $arr['url'] = $res['savepath'] . $res['savename'];
            } else {
                $this->error('文件上传失败');
            }
            $summary = mb_substr(strip_tags(htmlspecialchars_decode($arr['content'])), 0, 100, 'utf-8');
            $arr['summary'] = $summary;
            $arr['edit_at'] = time();
            $res = $article->add($arr);
            if ($res) {
                $this->success('添加成功', U('blog'));
            } else {
                $this->error('添加失败', U('addBlog'));
            }
        } else {
            $this->error('请登录后操作', U('login'));
        }

    }

    public function index()
    {
        $article = M('Article');
        $data = $article->limit(0, 3)->select();
        $this->assign('lists', $data);
        $this->display();
    }

    public function blog()
    {
        $article = M('Article');
        $p = I('get.page');
        $totlepage = $article->count();
        $page = new page($totlepage, 3);
        $page->setConfig('header', '共 %TOTAL_ROW% 条记录');
        $page->setConfig('theme', "%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER% %DOWN_PAGE%");
        $this->pager = $page;
        $data = $article->limit($p, 3)->select();
        $this->assign('lists', $data);
        $this->display();
    }

    public function delete()
    {
        if (session('?user_id')) {
            $article = M('Article');
            $id = $_GET['id'];
            if ($id) {
                $article->delete($id);
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        } else {
            $this->error('请登录后再操作', U('login'));
        }

    }

    public function read($id)
    {
        header('Content-type:text/html;charset=utf-8');
        $article = M('Article');
        $data = $article->find($id);
        $article->where("id=$id")->setInc('read_times');
        if ($data) {
            $this->assign('blog', $data);
        } else {
            $this->error('文章已被删除');
        }
        $comment = M('Comment');
        $lists = $comment->field("*,comment.id as id,user.id as uid")->where(['article_id'=>$id])->join('user on user_id = user.id')->select();
        //echo $comment->getLastsql();
        trace($lists);
        $this->assign('lists',$lists);
        $this->display();
        $a = M('Article');
        $arti = $a->where(['id'>50])->order('id desc')->limit(1)->find();
    }

    public function modify($id)
    {
        if (session('?user_id')) {
            $article = M('Article');
            $data = $article->find($id);
            if ($data) {
                $this->assign('blog', $data);
            } else {
                $this->error('文章已被删除');
            }
            $this->display();
        } else {
            $this->error('请登录后再操作', U('login'));
        }
    }


    public function modify_save()
    {
        header("Content-type: text/html; charset=utf-8");
        $article = M('Article');
        $data = $article->create();
        if ($data) {
            $article->save();
            $this->success('更新成功', U('blog'));
        } else {
            $this->error('更新错误');
        }
    }

    public function login_out()
    {
        session('user_id', null);
        $this->success('退出成功', U('login'));
    }

    public function comment()
    {
        header('Content-type:text/html;charset=utf-8');
        if (IS_AJAX && session('?user_id')) {
            $c = M('Comment');
            $arr = $c->create();
            $re = [];
            if ($arr) {
                $arr['user_id'] = session('user_id');
                $arr['create_at'] = time();
                $c->add($arr);
                $re['status'] = 1;
                $re['msg'] = '成功';
                $re['str'] = htmlspecialchars_decode($arr['content']);
            } else {
                $re['status'] = 0;
                $re['msg'] = '失败';
            }


        } else{
            $re['status'] = 0;
            $re['msg'] = '未登录';
        }
        $this->ajaxReturn($re);

//        echo IS_AJAX;
//        exit;
//        $c = M('Comment');
//        $arr = $c->create();
//        $content = strip_tags(htmlspecialchars_decode($arr['content']));
//        $arr['content'] = $content;
//        var_dump($arr);
//        $res = $c->add($arr);
//        var_dump($res);
    }

}